/**
* 
*  Processeur CT11MPCore avec DVS
* see <a href="{@docRoot}/Documentation/processor.html"> for more information</a>
*  Copyright (c) 2009 Ecole Centrale de Nantes IRCCyN
* @author: Urunuela Richard
* @version 3.2
*/

package storm.Processors;

import storm.Tasks.Task;
import log.DVSTrace;
/**
 * 
 * @author richard
 *
 */
public class CT11MPCore_DVS extends Processor {
	double [][]profil={{1,2000},{0.7,1000},{0.5,500}};
	/**
	 * 
	 */
	public CT11MPCore_DVS(){
		this.power = 1;
		this.sleep_power = 0.2;
	
		
	}
	/**
	 * 
	 */
	public int getNombrefreq() {
		return 3;
	}
	/**
	 * 
	 */
	public void setRunning(Task T){
		super.setRunning(T);
		if (this.isRunning()){
			this.majTask();
		}
		
	}
	/**
	 * 
	 */
	public int getDVSFactor() {
		// TODO Auto-generated method stub
		return (int) (profil[0][1]/profil[this.InternalState][1]);
	}
	/**
	 * 
	 */
	public double getPower(){
	 if (this.isRunning() ) {
		 return (profil[this.InternalState][0] * this.getrunning().powerDeviation /100);
	 }
	 else return sleep_power;
	}
	/**
	 * 
	 * @param n
	 */
	public void setInternalState(int n){
		this.InternalState=n;
		if (this.isRunning()){
		this.majTask();
		}
	}
	/**
	 * 
	 */
	public void onTick(){
		super.onTick();
		if (this.isRunning()){
			this.majTask();
		}
	}
	/**
	 * 
	 */
	public double getFreq(float dvsfactorNew) {
		// TODO Auto-generated method stub
		return (this.profil[0][1]/dvsfactorNew);
	}
	/**
	 * 
	 */
	private void majTask() {
		/**
		 * On change le ret en fonction de la fr??quence :)
		 */
		double freqMax = this.profil[0][1];
		double currentFreq = this.profil[this.InternalState][1];
		Task T = this.getrunning();
		float dvsfactor = (float) (freqMax/currentFreq);
		float r = T.getRET();
		if (T.getDvsFactor()!=dvsfactor) {
			r = r / T.getDvsFactor();
			r = r * dvsfactor;
			/**
			 * Log DVS 
			 */
			DVSTrace trace = new DVSTrace(this.Kernel.getTime(), T, this,T.getDvsFactor() ,dvsfactor, T.getRET(), r);
			this.Kernel.getLogs().addTrace(trace);
			T.setRet((int) r);
			T.setDvsFactor(dvsfactor);
		
		}
	}
}
